package com.app.netty;

import io.netty.channel.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.net.InetSocketAddress;
import java.util.concurrent.ConcurrentHashMap;


@Slf4j
@Component
@ChannelHandler.Sharable
public class NettyServerHandler extends ChannelInboundHandlerAdapter {

        private static final ConcurrentHashMap<String, ChannelHandlerContext> CHANNEL_MAP = new ConcurrentHashMap<>();
//    private ChannelHandlerContext ctx;

    /*   @Override
       public void channelRead(ChannelHandlerContext ctx, Object msg) {
           log.info("收到消息：{}", msg);
           String hostString = ((InetSocketAddress) ctx.channel().remoteAddress()).getHostString();

           log.info("收到消息：{}", hostString);
           System.out.println(msg);
       }*/
   /* @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        this.ctx = ctx;
    }*/

/*    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        super.channelRead(ctx, msg);
        log.info("thread.name={}", Thread.currentThread().getName());

        ByteBuf in = (ByteBuf) msg;

        String readStr = in.toString(CharsetUtil.UTF_8);
        log.info("Server received: {}", readStr);

        log.info("release msg");
        ReferenceCountUtil.release(msg);
    }*/



    /**
     * 本项目实现了一个简单的session管理
     **/
    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        InetSocketAddress insocket = (InetSocketAddress) ctx.channel().remoteAddress();
        //获取连接通道唯一标识
        String channelId = ctx.channel().id().asLongText();
        String hostString = ((InetSocketAddress) ctx.channel().remoteAddress()).getHostString();
        int port = ((InetSocketAddress) ctx.channel().remoteAddress()).getPort();
        //如果map中不包含此连接，就保存连接
        if (!CHANNEL_MAP.containsKey(channelId)) {
            CHANNEL_MAP.put(channelId, ctx);
//            log.info("新的连接加入了：[{}]", channelId);
            log.info("新的连接加入了：[{}]", hostString + ":" + port);

        }
        ctx.channel().read();
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) {
        String channelId = ctx.channel().id().asLongText();
        String hostString = ((InetSocketAddress) ctx.channel().remoteAddress()).getHostString();
        int port = ((InetSocketAddress) ctx.channel().remoteAddress()).getPort();
        if (CHANNEL_MAP.containsKey(channelId)) {
            //删除连接
            CHANNEL_MAP.remove(channelId);
            log.info("连接已断开：[{}]", hostString + ":" + port);
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);
        Channel channel = ctx.channel();
        if (channel.isActive()) {
            ctx.close();
        }
    }



}
